package middle;

import java.util.ArrayList;
import java.util.List;

public class Solution_1262 {

    public int maxSumDivThree(int[] nums) {
        int sum = 0;
        int max_zero_sum = 0, max_one_sum = 0, max_two_sum = 0;
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int mod = num % 3;
            if (mod == 0) {
                max_zero_sum += num;
                if (max_one_sum > 0) {
                    max_one_sum += num;
                }
                if (max_two_sum > 0) {
                    max_two_sum += num;
                }
            } else if (mod == 1) {
                int old_zero = max_zero_sum;
                int old_one = max_one_sum;
                int old_two = max_two_sum;
                max_one_sum = Math.max(max_zero_sum + num, old_one);
                if (old_one > 0) {
                    max_two_sum = Math.max(old_one + num, old_two);
                }
                if (old_two > 0) {
                    max_zero_sum = Math.max(old_two + num, old_zero);
                }
            } else {
                int old_zero = max_zero_sum;
                int old_one = max_one_sum;
                int old_two = max_two_sum;
                max_two_sum = Math.max(max_zero_sum + num, old_two);
                if (old_one > 0) {
                    max_zero_sum = Math.max(old_one + num, old_zero);
                }
                if (old_two > 0) {
                    max_one_sum = Math.max(old_two + num, old_one);
                }
            }
            System.out.println("zero:" + max_zero_sum + ";    one:" + max_one_sum + ";    two:" + max_two_sum);
            sum = Math.max(max_zero_sum, sum);
        }
        return max_zero_sum;
    }

    public static void main(String[] args) {
        Solution_1262 model = new Solution_1262();
        List<Integer> list = new ArrayList<>();
        list.add(20);
        list.add(0);
        list.add(90);
        list.add(10);
        list.sort((a, b) -> b - a);
        System.out.println(list);
//        System.out.println(model.maxSumDivThree(new int[]{8,5,4,9,2,9,10,6}));
//        System.out.println(model.maxSumDivThree(new int[]{5, 2, 2, 2}));
//        System.out.println(model.maxSumDivThree(new int[]{3, 6, 5, 1, 8}));
//        System.out.println(model.maxSumDivThree(new int[]{4}));
//        System.out.println(model.maxSumDivThree(new int[]{1, 2, 3, 4, 4}));
    }
}
